package com.sihan.idc.manager.controller;

import com.alibaba.fastjson.JSON;
import com.sihan.idc.manager.common.resp.Result;
import com.sihan.idc.manager.entity.BaseIotDataEntity;
import com.sihan.idc.manager.entity.DeviceBaseEntity;
import com.sihan.idc.manager.entity.DeviceIotDataEntity;
import com.sihan.idc.manager.service.DeviceBaseService;
import com.sihan.idc.manager.service.impl.RedisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜗牛数字化
 * @since 2024-08-24
 */
@Tag(name = "用户")
@RestController
@RequestMapping("/deviceBaseEntity")
public class DeviceBaseController {
    @Autowired
    private DeviceBaseService deviceService;

    @Autowired
    private RedisService redisService;

    @Operation(summary = "根据设备ID查询设备信息")
    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    public Result get(@PathVariable int id) {
        return Result.success(deviceService.getById(id));
    }
    @RequestMapping(value = "redis/refresh", method = RequestMethod.GET)
    public Result refresh() {
        List<BaseIotDataEntity> dataList = deviceService.queryAll(); // 替换为你的查询方法
        redisService.setValue("deviceList", JSON.toJSONString(dataList));
        dataList.forEach(data -> redisService.setValue(data.getSn(), data));
        return Result.success();
    }
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result add(@RequestBody DeviceBaseEntity device) {
        deviceService.save(device);
        return Result.success();
    }
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result list() {
        String json = (String) redisService.getValue("deviceList");
        List<BaseIotDataEntity> dataList = JSON.parseArray(json, BaseIotDataEntity.class);
        return Result.success(dataList);
    }

    @RequestMapping(value = "start/frpc/{id}", method = RequestMethod.GET)
    public Result startFrpc(@PathVariable int id) {

        return Result.success();
    }
    @RequestMapping(value = "stop/frpc/{id}", method = RequestMethod.GET)
    public Result stopFrpc(@PathVariable int id) {

        return Result.success();
    }
}
